﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataLayer;
using ExceptionLayer;

namespace CtrLayer
{
    public class AuctionCtrl
    {
        public void CreateAuction(double minPrice, double minBid, DateTime startDate, DateTime endDate, int artId, int timeInterval, int timeExtend)
        {
            if (minPrice >= 0.1)
            {
                if (minBid >= 1)
                {
                    if (startDate.CompareTo(endDate) < 0)
                    {
                        Auction newAuc = new Auction { MinPrice = (decimal)minPrice, MinBid = (decimal)minBid, StartDate = startDate, EndDate = endDate, Art = artId, TimeInterval = timeInterval, TimeExtend = timeExtend };

                        try
                        {

                            DBCon.GetDB().Auctions.InsertOnSubmit(newAuc);
                            DBCon.GetDB().SubmitChanges();
                        }
                        catch
                        {
                            
                        }
                    }
                    else
                    {
                        throw new UCNNotValidDateException();
                    }
                }
                else
                {
                    throw new UCNNotValidMinBetException();
                }
            }
            else
            {
                throw new UCNNotValidMinPriceException();
            }
        }

        public void AddArtToAuction(int artId, int aucId)
        {

            Auction updAuc = (from x in DBCon.GetDB().Auctions
                              where x.ID == aucId
                              select x).Single();

            if (aucId != null)
            {
                updAuc.Art = artId;

                DBCon.GetDB().SubmitChanges();
            }
            else
            {
                throw new UCNEmptyException();
            }
       }

        public List<Auction> GetAllAuctions()
        {
            List<Auction> list = new List<Auction>();

            var query = (from x in DBCon.GetDB().Auctions
                         select x);

           return query.ToList();

        }

        public void DeleteAuction(int id)
        {
            Auction delAuc = (from x in DBCon.GetDB().Auctions
                              where x.ID == id
                              select x).Single();

            if (delAuc != null)
            {
                DBCon.GetDB().Auctions.DeleteOnSubmit(delAuc);
                DBCon.GetDB().SubmitChanges();
            }
            else
            {
                throw new UCNEmptyException();
            }
        }

        public void UpdateAuction(int id ,double minPrice, double minBid, DateTime startDate, DateTime endDate, int timeInterval, int timeExtend)
        {
            Auction updAuc = (from x in DBCon.GetDB().Auctions
                              where x.ID == id
                              select x).Single();

            updAuc.MinPrice = (decimal)minPrice;
            updAuc.MinBid = (decimal)minBid;
            updAuc.StartDate = startDate;
            updAuc.EndDate = endDate;
            updAuc.TimeInterval = timeInterval;
            updAuc.TimeExtend = timeExtend;

            DBCon.GetDB().SubmitChanges();
        }

    }
}